﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Globalization;
using System.Resources;

namespace Common
{
    public class ForexBusinessHours
    {
        Dictionary<DateTime, DateTime> _holidays = new Dictionary<DateTime, DateTime>();

        public ForexBusinessHours()
        {

            ResourceSet _h = Holidays.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);

            foreach (DictionaryEntry entry in _h)
            {
                string resourceKey = entry.Key.ToString();
                object resource = entry.Value.ToString();

                string[] _r = resource.ToString().Split('^');

                DateTime _new = new DateTime(2000, Convert.ToInt32(_r[0]), Convert.ToInt32(_r[1]));

                _holidays.Add(_new, _new);
            }


        }

        public bool IsTradingClosed(DateTime time)
        {
            bool res = false;

            // EST Times
            // London 3am - 12am
            // NYC 8am - 5pm
            // Tokyo 7pm - 4am
            // Sydney 5pm - 2am
            // Berlin 2am - 11am


            

            // FX is closed from about 4:30 pm EST on Friday until (Sunday around 5 pm EST).
            if (time.DayOfWeek == DayOfWeek.Saturday)
                res = true;

            if (time.DayOfWeek == DayOfWeek.Friday && time.Hour >= 17)
                res = true;

            if (time.DayOfWeek == DayOfWeek.Sunday && time.Hour < 17)
                res = true;

            if (res == false)
            {
                DateTime _h = new DateTime(2000, time.Month, time.Day);

                if (_holidays.ContainsKey(_h))
                    res = true;
            }

            return res;
        }
    }
}
